package com.kzw.oa.exam.web;

import java.util.ArrayList;
import java.util.List;

import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.kzw.core.orm.StringPropertyFilter;
import com.kzw.oa.comm.model.TongBao;
import com.kzw.oa.comm.service.TongBaoService;
import com.kzw.oa.exam.service.ExamineService;

@Controller
@RequestMapping("/exam/stats/")
public class StatsAction {

    @Autowired
    private TongBaoService tongbaoService;
    @Autowired
    private ExamineService examineService;

    /**
     * 首页
     */
    @RequestMapping("console")
    public String console(Model model) {
        model.addAttribute("name", "zhc");
        try {
            // 获得服务器信息
            Sigar sigar = new Sigar();
            //JDK版本
            model.addAttribute("javaVersion", System.getProperty("java.version"));
            CpuInfo[] cpuInfoList = sigar.getCpuInfoList();
            //CPU信息
            model.addAttribute("cpu", cpuInfoList[0].getVendor() + "(" + cpuInfoList[0].getModel() + ") " + cpuInfoList[0].getMhz() + "MHz x " + cpuInfoList.length + "核");
            //操作系统
            String osname = System.getProperties().getProperty("os.name");
            model.addAttribute("osname", osname);

            //服务器内存
            Mem freeMemory = sigar.getMem();
            model.addAttribute("mem", String.format("%.2fG", new Object[]{Double.valueOf((double) freeMemory.getTotal() / 1.073741824E9D)}));
            //JVM内存
            model.addAttribute("jvmMaxMem", String.format("%.2fM", new Object[]{Float.valueOf((float) Runtime.getRuntime().maxMemory() / 1048576.0F)}));
            //内存使用率
            model.addAttribute("memUsedPercent", String.format("%.2f", new Object[]{Double.valueOf(freeMemory.getUsedPercent())}));
            //CPU使用使用率
            model.addAttribute("cpuUsed", String.format("%.2f", new Object[]{Double.valueOf(CpuPerc.format(sigar.getCpuPerc().getCombined()).replace("%", ""))}));
            long pid = sigar.getPid();
            long startTime = sigar.getProcTime(pid).getStartTime();
            //系统运行时间
            model.addAttribute("runningTime", (System.currentTimeMillis() - startTime) / 60000);
            //系统内存使用
            long maxMemory2 = Runtime.getRuntime().maxMemory();
            long freeMemory1 = Runtime.getRuntime().freeMemory();
            //JVM内存使用
            model.addAttribute("useableMemeory", String.format("%.2f", new Object[]{Double.valueOf((double) (maxMemory2 - freeMemory1) / ((double) maxMemory2 * 1.0D) * 100.0D)}));
        } catch (SigarException e) {
            System.err.println("使用sigar获取服务器信息错误：" + e);
        }

        // 进行中的考试场次，已完成考试场次，未开始的考试场次
        String hql = "select count(*) from Examine where status=?";
        Long cnt11 = examineService.findUniqueByHQL(hql, 0);
        model.addAttribute("cnt11", cnt11);
        
        Long cnt12 = examineService.findUniqueByHQL(hql, 1);
        model.addAttribute("cnt12", cnt12);
        
        Long cnt13 = examineService.findUniqueByHQL(hql, 2);
        model.addAttribute("cnt13", cnt13);
        
        // 参加考试人数，已提交考卷人数，未提交考卷人数
        String hql21 = "select count(*) from ExamResult where examine.oid=?";
        Long cnt21 = examineService.findUniqueByHQL(hql21, 1L);
        model.addAttribute("cnt21", cnt21);
        
        String hql22 = "select count(*) from ExamResult where examine.oid=? and status=?";
        Long cnt22 = examineService.findUniqueByHQL(hql22, 1L, 1);
        model.addAttribute("cnt22", cnt22);
        
        Long cnt23 = examineService.findUniqueByHQL(hql22, 1L, 0);
        model.addAttribute("cnt23", cnt23);
        
//      通知公告：本单位可见
        List<StringPropertyFilter> filters = new ArrayList<>();
        List<TongBao> tongbaoList = tongbaoService.listMy(filters);
        model.addAttribute("tongbaoList", tongbaoList);

        return "stats/console";
    }

}
